package com.lwj.liwanjia.view.fragment.myindent;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.baoyz.widget.PullRefreshLayout;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.indent.UnPaidAdpater;
import com.lwj.liwanjia.base.LazyFragment;
import com.lwj.liwanjia.bean.MyIndentBean;
import com.lwj.liwanjia.config.OnClickPosition;
import com.lwj.liwanjia.config.OnItemClickListener;
import com.lwj.liwanjia.impl.MyIndentView;
import com.lwj.liwanjia.presenter.MyIndentPresenter;
import com.lwj.liwanjia.utils.SpfUtils;
import com.lwj.liwanjia.view.activity.indent.UnpaidActivity;
import com.lwj.liwanjia.view.activity.login.LoginActivity;
import com.lwj.liwanjia.view.activity.myindent.BanjiaDetailActivity;
import com.lwj.liwanjia.view.activity.myindent.BinGuanActivity;
import com.lwj.liwanjia.view.activity.myindent.BuyDetailActivity;
import com.lwj.liwanjia.view.activity.myindent.JiuDianDetailActivity;
import com.lwj.liwanjia.view.activity.myindent.KuaidiDetailActivity;
import com.lwj.liwanjia.view.activity.myindent.LvYouDetailActivity;
import com.lwj.liwanjia.view.activity.myindent.QuDetailActivity;
import com.lwj.liwanjia.view.activity.myindent.SendDetailActivity;
import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 待付款
 */
public class UnPaidFragment extends LazyFragment implements MyIndentView, PullRefreshLayout.OnRefreshListener {
    @BindView(R.id.unpaid_rlv)
    RecyclerView unpaidRlv;
    @BindView(R.id.progress)
    ProgressBar progressBar;
    @BindView(R.id.no_indent)
    TextView noIndent;
    @BindView(R.id.refressh)
    PullRefreshLayout refreshLayout;

    // 标志位，标志已经初始化完成。
    private boolean isPrepared;
    private UnPaidAdpater unPaidAdpater;
    //private List<String> list = new ArrayList<>();测试数据
    private List<MyIndentBean.DataBean> list = new ArrayList<>();
    private MyIndentPresenter myIndentPresenter;
    private String token;
    private int circleType = 0;
    private int clickPos = 0;

    @Override
    protected void lazyLoad() {
        if (!isPrepared || !isVisible) {
            return;
        }
        initData();
    }

    private void initData() {
        myIndentPresenter.getIndentData(token, "待付款");
    }

    @Override
    protected void initView() {
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        unpaidRlv.setLayoutManager(manager);
        unPaidAdpater = new UnPaidAdpater(getActivity(), list, false);
        unpaidRlv.setAdapter(unPaidAdpater);
        myIndentPresenter = new MyIndentPresenter(this);
        token = SpfUtils.getSpfUtils(getActivity()).getToken();
        refreshLayout.setOnRefreshListener(this);
        litener();
        isPrepared = true;
        lazyLoad();
    }

    private void litener() {
        unPaidAdpater.setListener(new OnItemClickListener() {
            @Override
            public void OnItemClick(View view, int position) {
                clickPos = position;
                Intent intent = new Intent(getActivity(), UnpaidActivity.class);
                intent.putExtra("tradeId", list.get(position).getGoods().getTradeId());
                startActivity(intent);
            }
        });
        unPaidAdpater.setClickPosition(new OnClickPosition() {
            @Override
            public void clickPosition(int position) {
                clickPos = position;
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_un_paid;
    }

    @Override
    public void showProgress() {
        if (circleType == 0) {
            progressBar.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void hideProgress() {
        progressBar.setVisibility(View.GONE);
    }

    @Override
    public void showError(Exception e) {
        Toast.makeText(getActivity(), "数据加载失败", Toast.LENGTH_SHORT).show();
        refreshLayout.setRefreshing(false);
    }

    /**
     * 获取所有订单
     *
     * @param myIndentBean
     */
    @Override
    public void getIndentData(MyIndentBean myIndentBean) {
        if (myIndentBean.getState() == 0) {
            Toast.makeText(getActivity(), "访问有误", Toast.LENGTH_SHORT).show();
        } else {
            list.clear();
            list.addAll(myIndentBean.getData());
            if (list.size() > 0) {
                unpaidRlv.setVisibility(View.VISIBLE);
                noIndent.setVisibility(View.GONE);
                unPaidAdpater.notifyDataSetChanged();
            } else {
                unpaidRlv.setVisibility(View.GONE);
                noIndent.setVisibility(View.VISIBLE);
            }
        }
        unpaidRlv.scrollToPosition(clickPos);
        circleType = 0;
        refreshLayout.setRefreshing(false);
    }

    @OnClick(R.id.no_indent)
    public void onViewClicked() {
        initData();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (myIndentPresenter != null) {
            myIndentPresenter = null;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unPaidAdpater.cancelAllTimers();
    }

    @Override
    public void onRefresh() {
        circleType = 1;
        initData();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isPrepared && isVisible) {
            initData();
        }
    }
}
